<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="public/css.css" rel="stylesheet" type="text/css" />
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <title>web2</title>
    </head>

    <body>
        <div class="chung">
            <?php
            require_once './model/works.php';
            require_once './model/users.php';
            $keyword = new Works();
            $keyword->table_name = "works";
            ?>
            <?php
            if (!isset($_GET["page"]))
                $page = 1;
            else
                $page = $_GET["page"];

            $total = $keyword->countWords();
            ?>
            <?php include './html/header.php'; ?>
            <div class="menu">
                <ul>
                    <li><a href="index.php"> Menu</a></li>
                    <li><a href="#"> Content types</a></li>
                    <li><a href="#"> Forums</a></li>
                    <li><a href="#"> Contact</a></li>
                    <li> <input type="text" id="type"/><input type="button" value="go" id="ty"></li>
                </ul>
                <div class="thanh"></div>

            </div>

            <div class="noidung">
                
                <hr id="like"></hr>
                <div class="nav" style="clear: both;"> 
                    <ul>
                        <?php if (!isset($_GET["page"])):
                            ?>

                            <a href="index.php?page=<?php echo $_GET['page'] = 1 ?>">
                            </a>
                        <?php endif; ?>


                        <li> <a href="index.php?page=1">Fist</a></li>
                        <li><a href="index.php?page=<?php echo $_GET["page"] - 1 ?>"><< Prev</a></li>

                        <?php for ($i = 1; $i <= 7; $i++): ?>
                            <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                                <?php for ($i = 1; $i <= 7; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php elseif (($_GET["page"] == $total / PAGE) || ($_GET["page"] == $total / PAGE - 1) || ($_GET["page"] == $total / PAGE - 2) || ($_GET["page"] == $total / PAGE - 3)): ?>        
                                <?php for ($i = $total / PAGE - 7; $i <= $total / PAGE; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php else : ?>
                                <?php for ($i = $_GET["page"] - 3; $i <= $_GET["page"] + 3; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php endif; ?> 
                        <?php endfor; ?>
                        <li><a href="index.php?page=<?php echo $_GET['page'] + 1 ?>">Next >></a></li>           
                        <li><a href="index.php?page=<?php echo $total / PAGE ?>">Last</a></li>

                        <?php $keywords = $keyword->select($page); ?>
                    </ul>
                </div>
                <div class="left">
                <h3>MAIN NAME</h3> 
                <hr id="likeleft"></hr>
            <div class="ndleft">
                <h2>LIST</h2>
                <ul>
                    <li><a href="#"> Menu</a></li>
                    <li><a href="#"> Content types</a></li>
                    <li><a href="#"> Forums</a></li>
                    <li><a href="#"> Contact</a></li>
                </ul>
                </div>
<form method="POST"> 
    <table>
        <tr>
            <td>Username: <br/>
                <input type="text" name="username" id="name" value="" placeholder="Username" ></td>
        </tr>
        <tr>
            <td>Password: <br/>
                <input type="password" name="password" id="password" placeholder="pasword"></td>
        </tr>
    </table>
    <input type="submit" name="submit" value="Enter"></p>
</form>
 
<?php if (isset($_POST['submit'])): ?>
    <?php
    $username = $_POST['username'];
    $password = $_POST['password'];
    $Ok = FALSE;
    $index = -1;
    for ($i = 0; $i < count($users); $i++) {
        if ((strcmp($users[$i]['username'], $username) == 0) &&
                (strcmp($users[$i]['password'], $password) == 0)) {
            $Ok = TRUE;
            $index = $i;
        }
    }
    if ($Ok) {
        header("Location: http://localhost/htbvan-web2/index.php?uid=" . $users[$index]["id"]);
    } else {
        echo "Đăng nhập thất bại";
    }
    ?>
<?php endif; ?>
        
             </div>   
                
                <div class="ndr">
                    <h3>Works</h3>
                    <hr id="likeri"></hr>
                <div class="ajax_show"></div>
                <table border="1px" >
                    <tr  id="tr" >
                        <td>Id</td>
                        <td>Name</td>
                        <td>image</td>
                        <td>Description</td>
                        <td>Oparetion</td>
                    </tr>
                    <?php foreach ($keywords as $keywordss): ?>
                        <tr class="b">
                            <td><?php echo $keywordss['id']; ?></td>
                            <td><?php echo $keywordss['name']; ?></td>
                            <td><img src="<?php echo $keywordss['image']; ?> " width='100px' height='100px'></td>
                            <td><?php echo substr($keywordss['description'], 0, 100); ?></td>
                            <td><img src="public/images/edit.png" width="15px" height="15px"><a href="submit.php?id=<?php echo $keywordss['id']; ?>">Edit</a>
                                <img src="public/images/view.png" width="15px" height="15px"><a href="#" class="ajax_view" id_keyword="<?php echo $keywordss['id'].'<br>'.$keywordss['name'].'<br>'.substr($keywordss['description'], 0, 1000); ?>">View</a><br>
                                <img src="public/images/delete.png" width="15px" height="15px"><a href="submit.php">Delete</a></td>
                        </tr>
                    <?php endforeach; ?>
                </table>
                        </div>
                   
            </div>
            
            <?php include './html/footer.php'; ?>

        </div>
 <script type="text/javascript">
                        $(document).ready(function(){
                            $('.ajax_view').click(function(){      
                                id_keyword = $(this).attr('id_keyword');
                                url = 'ajax.php?id_keyword='+id_keyword;
                                $.ajax({
                                    url: url,                                    
                                    }).done(function(data) {
                                        $('.ajax_show').html(data);
                                    });
                            });
                        });
        </script>
    </body>
</html>